/**
 * @file   GetClientMethodParameterInfoRequestPacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-05-30
 * @brief  DataBus Get Client Method Parameter Info Request packet.
 *
 * Copyright (C) 2013 Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DataBus/Common/GetClientMethodParameterInfoRequestPacket.h>

bool DataBus::GetClientMethodParameterInfoRequestPacket::create(const quint8 source,
                                                                const quint8 destination,
                                                                const quint8 packetId,
                                                                const quint8 methodId,
                                                                const quint8 parameterIndex,
                                                                Packet *packet)
{
    // Check parameters
    if ((destination == 0) ||
        (packet == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Create Header
    packet->setSource(source);
    packet->setDestination(destination);
    packet->setPacketType(PacketType_GetClientMethodParameterInfoRequest);
    packet->setPacketId(packetId);

    // Create Payload
    packet->setData(QByteArray(1, static_cast<char>(methodId)));
    packet->setData(QByteArray(1, static_cast<char>(parameterIndex)));

    // Success
    return true;
}

bool DataBus::GetClientMethodParameterInfoRequestPacket::parse(const Packet &packet,
                                                               quint8 *methodId,
                                                               quint8 *parameterIndex)
{
    // Check parameters
    if ((packet.getDestination() == 0) ||
        (packet.getPacketType() != PacketType_GetClientMethodParameterInfoRequest) ||
        (methodId == 0) ||
        (parameterIndex == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Get data
    QByteArray data = packet.getData();

    if (data.size() != 2)
    {
        // Error, invalid size
        return false;
    }

    // Get Method ID
    *methodId = static_cast<quint8>(data.at(0));

    // Get Parameter Index
    *parameterIndex = static_cast<quint8>(data.at(1));

    // Success
    return true;
}
